package corba;

import helloworld.Hello;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;

public class HelloServer {
	public static void main(String args[]) {
		try {
			// 根据端口创建ORB实例
			String str[] = { "-ORBInitialPort", "1050" };
			ORB orb = ORB.init(str, null);
			org.omg.CORBA.Object objRef = orb
					.resolve_initial_references("RootPOA");
			
			// 创建服务对象
			org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper
					.narrow(objRef);
			org.omg.PortableServer.POAManager manager = rootPOA
					.the_POAManager();
			
			// 具体实现类
			HelloImpl helloImpl = new HelloImpl();
			Hello hello = helloImpl._this(orb);
			
			// 创建命名并注册
			NamingContext ncRef = NamingContextHelper.narrow(orb
					.resolve_initial_references("NameService"));
			NameComponent nc = new NameComponent("Hello", "");
			NameComponent path[] = { nc };
			ncRef.rebind(path, hello);
			manager.activate();
			System.out.println("服务器正在运行...");
			
			// 等待客户端调用
			orb.run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
